/*
 *  CEListItem.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_ListItem_h__
#define __CE_ListItem_h__

#include "cocos2dx_extentions/controls/CEMenuItemSprite.h"

#include <vector>

namespace cocosext {

class CEListItem;
typedef std::vector<CEListItem*> CEListItems;

/////////////////////////////////////////////////////
//
/** CEListItemInfo 
* 
*/
/////////////////////////////////////////////////////

struct CEListItemInfo
{
	float marginLeft; //Left margin
	float marginRight; //Right margin is used only when (isAutoResizable == true)
	bool isAutoResizable; //Item is resized by the list when list is resized

public:
	CEListItemInfo(): marginLeft(0.f), marginRight(0.f), isAutoResizable(false) {;}
	CEListItemInfo(float marginLeft_, float marginRight_, bool isAutoResizable_)
		: marginLeft(marginLeft_), marginRight(marginRight_), isAutoResizable(isAutoResizable_) {;}
};

/////////////////////////////////////////////////////
//
/** CEListItem 
* 
*/
/////////////////////////////////////////////////////

class CEVerticalListView;

class CEListItem : public cocosext::CEMenuItemSprite
{
friend class CEVerticalListView;

protected:
	CEVerticalListView* _list; // weak ref
	float _marginLeft; //Left margin
	float _marginRight; //Right margin is used only when (isAutoResizable == true)
	bool _isAutoResizable; //Item is resized by the list when list is resized

protected:
	CEListItem(const CEListItemInfo& info)
		: _list(NULL)
		, _marginLeft(info.marginLeft)
		, _marginRight(info.marginRight)
		, _isAutoResizable(info.isAutoResizable)  {;}

public:
    virtual ~CEListItem() {;}

    /* Static constructors */
    /** creates a menu item with a normal, selected and disabled image*/
    static CEListItem* itemFromNormalSprite(const CEListItemInfo& info, cocos2d::CCNode* normalSprite, cocos2d::CCNode* selectedSprite, cocos2d::CCNode* disabledSprite = NULL);
    /** creates a menu item with a normal and selected image with target/selector */
    static CEListItem* itemFromNormalSprite(const CEListItemInfo& info, cocos2d::CCNode* normalSprite, cocos2d::CCNode* selectedSprite, cocos2d::CCObject* target, cocos2d::SEL_MenuHandler selector);
    /** creates a menu item with a normal,selected  and disabled image with target/selector */
    static CEListItem* itemFromNormalSprite(const CEListItemInfo& info, cocos2d::CCNode* normalSprite, cocos2d::CCNode* selectedSprite, cocos2d::CCNode* disabledSprite, cocos2d::CCObject* target, cocos2d::SEL_MenuHandler selector);

public:
    /** initializes a menu item with a normal, selected  and disabled image with target/selector */
    bool initFromNormalSprite(cocos2d::CCNode* normalSprite, cocos2d::CCNode* selectedSprite, cocos2d::CCNode* disabledSprite, cocos2d::CCObject* target, cocos2d::SEL_MenuHandler selector);

public:
	//Main interface
	float marginLeft() const { return _marginLeft; }
	float marginRight() const { return _marginRight; }
	bool isAutoResizable() const { return _isAutoResizable; }

	virtual void setWidth(float width);

protected:
	virtual void onAfterAdd(CEVerticalListView* list);
	virtual void onBeforeRemove();

}; //CEListItem




/////////////////////////////////////////////////////
//
/** CEListItemWithText 
* It is a mix of list item and label
*/
/////////////////////////////////////////////////////

class CEListItemWithText : public CEListItem, public CELabelProtocol
{
protected:
	CELabelProtocol* _pNormalLabel;
    CELabelProtocol* _pSelectedLabel;
    CELabelProtocol* _pDisabledLabel;

protected:
	CEListItemWithText(const CEListItemInfo& info): CEListItem(info), _pNormalLabel(NULL), _pSelectedLabel(NULL), _pDisabledLabel(NULL) {;}

public:
    virtual ~CEListItemWithText() {;}

    /* Static constructors */
    /** creates a menu item with a normal,selected  and disabled image with target/selector */
    static CEListItemWithText* itemFromNormalSprite(const CEListItemInfo& info, cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
							  cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
							  cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
							  cocos2d::CCObject* target, 
							  cocos2d::SEL_MenuHandler selector);

public:
    /** initializes a menu item with a normal, selected  and disabled image with target/selector */
    bool initFromNormalSprite(cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
							  cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
							  cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
							  cocos2d::CCObject* target, 
							  cocos2d::SEL_MenuHandler selector);


public:
//  CELabelProtocol interface 
virtual void setString(const wchar_t *str);
virtual const wchar_t* getString(void) const;

}; //CEListItemWithText


}; // end namespace cocosext

#endif // __CE_ListItem_h__
